import { collectPage } from '../../../api/webCast'
import { collectTeachingResearch } from '../../../api/teachingResearch'
import { pageCollect } from '../../../api/prepareLesson'

Page({
  data: {
    activeName: 'live',
    isLiveEnd: false,
    liveList: [],
    liveQuery: {
      condition: {},
      pager: {
        page: 1,
        pageSize: 10
      }
    },
    isPrepareEnd: false,
    prepareList: [],
    prepareQuery: {
      condition: {},
      pager: {
        page: 1,
        pageSize: 10
      }
    },
    isActivityEnd: false,
    activityList: [],
    activityQuery: {
      condition: {},
      pager: {
        page: 1,
        pageSize: 10
      }
    }
  },

  onLoad: function (options) {
    this.getLiveList()
    this.getActivityList()
    this.getPrepareList()
  },

  getLiveList() {
    if (!this.data.isLiveEnd) {
      collectPage(this.data.liveQuery).then(res => {
        if (res.records.length) {
          let list = this.data.liveList
          list = list.concat(res.records)
          this.setData({
            liveList: list
          })
        }
        if (res.records.length < 10) {
          this.setData({
            isLiveEnd: true
          })
        }
      })
    }
  },

  getMoreLive() {
    if (!this.data.isLiveEnd) {
      const page = this.data.liveQuery.pager.page++
      this.setData({
        'liveQuery.pager.page': page
      })
      this.getLiveList()
    }
  },

  getActivityList() {
    if (!this.data.isActivityEnd) {
      collectTeachingResearch(this.data.activityQuery).then(res => {
        if (res.records.length) {
          let list = this.data.activityList
          list = list.concat(res.records)
          this.setData({
            activityList: list
          })
        }
        if (res.records.length < 10) {
          this.setData({
            isActivityEnd: true
          })
        }
      })
    }
  },

  getMoreActivity() {
    if (!this.data.isActivityEnd) {
      const page = this.data.activityQuery.pager.page++
      this.setData({
        'activityQuery.pager.page': page
      })
      this.getActivityList()
    }
  },

  getPrepareList() {
    if (!this.data.isPrepareEnd) {
      pageCollect(this.data.prepareQuery).then(res => {
        if (res.records.length) {
          let list = this.data.prepareList
          list = list.concat(res.records)
          this.setData({
            prepareList: list
          })
        }
        if (res.records.length < 10) {
          this.setData({
            isPrepareEnd: true
          })
        }
      })
    }
  },

  getMorePrepare() {
    if (!this.data.isPrepareEnd) {
      const page = this.data.prepareQuery.pager.page++
      this.setData({
        'prepareQuery.pager.page': page
      })
      this.getPrepareList()
    }
  },

  tapTabs(e) {
    this.setData({
      activeName: e.detail.name
    })
  },

  toLower() {
    switch(this.data.activeName) {
      case 'live':
        this.getMoreLive();
        break
      case 'activity':
        this.getMoreActivity();
        break
      case 'prepare':
        this.getMorePrepare();
        break
    }
  }
})
